package lib

func init() {
	Probs = append(Probs, Problem{
		Num:         213,
		Discription: "循环数组打家劫舍",
		Level:       2,
		Labels: map[string]int{
			"动态规划": 1,
		},
	})
}

func Rob2(nums []int) int {
	if len(nums) == 1 {
		return nums[0]
	}

	if len(nums) == 2 {
		return max(nums[0], nums[1])
	}

	robRange := func(start, end int) int {
		if start == end-1 {
			return max(nums[start], nums[end])
		}

		//偷窃到前两个节点的最大值
		preMax := nums[start]
		//偷窃到前一个节点的最大值
		curMax := max(nums[start], nums[start+1])
		for i := start + 2; i <= end; i++ {
			tempCurMax := curMax
			curMax = max(curMax, preMax+nums[i])
			preMax = tempCurMax
		}

		return max(preMax, curMax)
	}

	//分两个范围偷取最大
	return max(robRange(0, len(nums)-2), robRange(1, len(nums)-1))
}
